package com.kafka.utils;

import cn.hutool.db.Db;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
 * @Author: Jiangxx
 * @Date: 2022/01/13
 * @Description:
 */
public class DBUtils {

    private static final String URL = "jdbc:mysql://localhost:3306/kafka?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai";

    private static final String USERNAME = "root";

    private static final String PASSWORD = "root";

    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";

    private static Db db = null;

    public static Db getDB() {
        if (db == null) {
            HikariConfig config = new HikariConfig();
            config.setDriverClassName(DRIVER);
            config.setJdbcUrl(URL);
            config.setUsername(USERNAME);
            config.setPassword(PASSWORD);
            config.setMaximumPoolSize(Integer.parseInt(System.getenv().getOrDefault("MAX_POOL_SIZE", "10")));
            HikariDataSource dataSource = new HikariDataSource(config);
            db = Db.use(dataSource);
        }
        return db;
    }
}
